//
//  DDNews.m
//  NetEaseNews
//
//  Created by dqw on 15/7/24.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "DDNews.h"
#import "DDNetworkTool.h"
@implementation DDNews

- (instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+ (instancetype)newsWithDic:(NSDictionary *)dic
{
    return [[self alloc]initWithDic:dic];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}
+ (void)newsListWithUrlStr:(NSString *)urlStr completionB:(void(^)(NSArray *arr))completionB
{
    NSAssert(completionB, @"compltionB不能为nil");
    [DDNetworkTool sharedTool].responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json",@"text/javascript", @"text/html", nil];
    [[DDNetworkTool sharedTool] GET:urlStr parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary *dict) {
        
        NSString *firstKey = dict.keyEnumerator.nextObject;
        NSArray *arr = dict[firstKey];
        NSMutableArray *arrM = [[NSMutableArray alloc]initWithCapacity:arr.count];
        for (NSDictionary *dic in arr) {
            
            DDNews *news = [DDNews newsWithDic:dic];
            [arrM addObject:news];
        }
        completionB(arrM.copy);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}

@end
